package edu.mit.media.fv.components
{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.*;
	
	public class FVGlassScrollPane extends Sprite
	{	
		private var _glow:GlowFilter = new GlowFilter(0xFFFFFF,0.8,10,10);
		private var _mask:Sprite = new Sprite();
		private var scrollPane:Sprite = new Sprite();
		private var thumb:Sprite = new Sprite();
		private var handle:Sprite = new Sprite();
		private var barWidth:int = 40;
		private var offset:int = 6;
		private var _height:int;
		private var _width:int;
		private var _viewport:DisplayObject;
		
		public function FVGlassScrollPane(x:int=0, y:int=0, width:int=0, height:int=0){
			super();
			this.scrollPane.addChild(this._mask);
			this.scrollPane.mask = _mask;
			this.addChild(this.scrollPane);
			this.initHandle();
			this.setBounds(x, y, width, height);
		}
		
		public function setBounds(x:int, y:int, width:int, height:int):void{
			this._width = width;
			this._height = height;
			this.x = x;
			this.y = y;
			this.reform();
		}
		public function get viewport():DisplayObject{
			return this._viewport;
		}
		public function set viewport(o:DisplayObject):void{
			if(this.viewport != null){
				this.scrollPane.removeChild(this.viewport);
			}
			this._viewport = o;
			this.scrollPane.addChild(o);
		}
		
		
		private function initHandle():void{
			this.thumb.filters = [_glow];
			
			this.handle.addChild(this.thumb);
			this.addChild(this.handle);
			
			//listeners
			this.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbPressed);
			this.thumb.addEventListener(MouseEvent.MOUSE_MOVE, thumbDragged);
			this.addEventListener(MouseEvent.ROLL_OUT, thumbReleased);
			this.addEventListener(MouseEvent.MOUSE_UP, thumbReleased);
		}
		
		private function scrollUp(event:MouseEvent):void{
			if(this.viewport.y+10<0){
				this.viewport.y = this.viewport.y+10;
			}else{
				this.viewport.y = 0;
			}
		}
		private function scrollDown(event:MouseEvent):void{
			if(this.viewport.y>-1*(this.viewport.height-this._height)){
				this.viewport.y = this.viewport.y-10;
			}
		}

		private var isThumbPressed:Boolean = false;
		private function thumbPressed(event:MouseEvent):void{
			isThumbPressed = true;
			var bounds:Rectangle = new Rectangle(0,this.barWidth,0,this._height-this.barWidth*2-this.thumb.height);
			this.thumb.startDrag(false, bounds);
		}		
		private function thumbReleased(event:MouseEvent):void{
			if(isThumbPressed){
				isThumbPressed = false;
				this.thumb.stopDrag();
			}
		}
		private function thumbDragged(event:MouseEvent):void{
			if (isThumbPressed){
				var trackWidth:Number = this._height-this.barWidth*2;
				var offset:Number = this.thumb.y - this.barWidth;
				var newY:Number = (offset*this.viewport.height)/trackWidth;
				this.viewport.y = -newY;
			}
		}		
		
		public function reform():void{
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xFFFFFF, 1.0);
			_mask.graphics.drawRect(0,0,this._width-this.barWidth, this._height);
			_mask.graphics.endFill();
			
			this.handle.x = this._width-this.barWidth;
			this.handle.graphics.clear();
			//this.handle.graphics.lineStyle(1, 0xFFFFFF, 1.0, true);
			//FVUtilities.drawRect(this.handle.graphics, 0,0,this.barWidth, this._height);
			this.handle.graphics.beginFill(0x000000, 0.1);
			this.handle.graphics.lineStyle(1,0xFFFFFF,0.6,true);
			this.handle.graphics.drawRect(0,0,this.barWidth, this._height);
			this.handle.graphics.endFill();
			
			this.thumb.graphics.clear();
			this.thumb.graphics.beginFill(0xFFFFFF, 1.0);
			var thumbHeight:Number = 10;
			if(this.viewport == null || this.viewport.height == 0){
				thumbHeight = this._height-this.barWidth*2;
			}else{
				var thumbRatio:Number = this._height/Number(this.viewport.height);
				if(thumbRatio>1){
					thumbHeight = this._height-this.barWidth*2;
				}else{
					thumbHeight = thumbRatio*(this._height-this.barWidth*2);
				}
			}
			this.thumb.graphics.drawRect(2,2,this.barWidth-4,thumbHeight-4);
			this.thumb.graphics.endFill();
			this.thumb.x = 0;
			this.thumb.y = this.barWidth;
			
		}
		public function set background(o:Boolean):void{
			if(o){
				this.graphics.clear();
				this.graphics.beginFill(0xFFFFFF, 0.12);
				this.graphics.lineStyle(1, 0xFFFFFF, 0.70, true);
				this.graphics.drawRoundRect(-5,-5,this._width+10, this._height+10, 10,10);
				this.graphics.endFill();
			}else{
				this.graphics.clear();
			}
		}
	}
}